package com.aust.furniture.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aust.furniture.entity.Furniture;
import com.aust.furniture.entity.Owinfo;
import com.aust.furniture.mapper.FurnitureMapper;
import com.aust.furniture.mapper.OwinfoMapper;
import com.aust.furniture.service.IOwinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出入库service实现类
 */
@Service
public class OwinfoServiceImpl extends ServiceImpl<OwinfoMapper, Owinfo> implements IOwinfoService {
    @Autowired
    private OwinfoMapper owinfoMapper;
    @Autowired
    private FurnitureMapper furnitureMapper;

    /**
     * 分页查询家具出入库数据
     *
     * @param pageNum  第几页
     * @param pageSize 每页多少条数据
     * @param param    查询参数-家具出入库名称
     * @return
     */
    @Override
    public IPage<Owinfo> selectOwinfoPage(int pageNum, int pageSize, String param) {
        QueryWrapper<Owinfo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param)){
            queryWrapper.like("dname",param);
        }
        Page<Owinfo> page = new Page<>(pageNum,pageSize);
        return owinfoMapper.selectPage(page,queryWrapper);
    }

    /**
     * 新增一条家具出入库信息
     *
     * @param owinfo
     */
    @Transactional
    @Override
    public int addOwinfo(Owinfo owinfo) {
        owinfoMapper.insert(owinfo);
        //todo 同时修改商品库存信息
        String type = owinfo.getType();
        QueryWrapper<Furniture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",owinfo.getDname());
        Furniture furniture = furnitureMapper.selectOne(queryWrapper);
        Integer stock = furniture.getStock();//商品库存
        Integer newStock = 0;
        //判断出入库类型
        if(type.equals("入库")){
            //增加商品的库存
             newStock = stock+owinfo.getCount();
             
        }else{
            //出库 减少商品的库存
            if(stock<owinfo.getCount()){
                throw new RuntimeException("库存不足");
            }
             newStock = stock - owinfo.getCount();
        }
        UpdateWrapper<Furniture> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",furniture.getId()).set("stock",newStock);
        furnitureMapper.update(null,updateWrapper);
        return 1;
    }

    /**
     * 修改一条家具出入库信息
     *
     * @param owinfo
     */
    @Transactional //加事务
    @Override
    public int editOwinfo(Owinfo owinfo) {
        Integer owinfoId = owinfo.getId();
        Owinfo owinfoDB = owinfoMapper.selectById(owinfoId);
        Integer oldCount = owinfoDB.getCount();
        owinfoMapper.updateById(owinfo);
        //todo 同时修改商品库存信息
        String type = owinfoDB.getType();
        QueryWrapper<Furniture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",owinfo.getDname());
        Furniture furniture = furnitureMapper.selectOne(queryWrapper);
        Integer stock = furniture.getStock();//商品库存
        Integer newStock = 0;
        //判断出入库类型
        if(type.equals("入库")){
            //增加商品的库存
            newStock = stock-oldCount+owinfo.getCount();
        }else{
            //出库 减少商品的库存
            if(stock<owinfo.getCount()){
                throw new RuntimeException("库存不足");
            }
            newStock = stock + oldCount - owinfo.getCount();
        }
        UpdateWrapper<Furniture> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",furniture.getId()).set("stock",newStock);
        furnitureMapper.update(null,updateWrapper);
        return 1;
    }

    /**
     * 根据主键id查询一个家具出入库对象
     *
     * @param id
     * @return
     */
    @Override
    public Owinfo queryOwinfoById(Integer id) {
        return owinfoMapper.selectById(id);
    }

    /**
     * 根据主键id删除一个家具出入库对象
     *
     * @param id
     * @return
     */
    @Override
    public int delOwinfoById(Integer id) {
        //先查询出该出入库信息
        Owinfo owinfo = owinfoMapper.selectById(id);
        //根据出入库类型 修改商品库存信息
        String type = owinfo.getType();
        QueryWrapper<Furniture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",owinfo.getDname());
        Furniture furniture = furnitureMapper.selectOne(queryWrapper);
        Integer stock = furniture.getStock();//商品库存
        Integer newStock = 0;
        //判断出入库类型
        if(type.equals("入库")){
            //删除入库信息 原库存减少
            newStock = stock - owinfo.getCount();
            if(newStock < 0){
                newStock = 0;
            }
        }else{
            //删除出库信息 库存增加
            newStock = stock + owinfo.getCount();
        }
        UpdateWrapper<Furniture> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",furniture.getId()).set("stock",newStock);
        furnitureMapper.update(null,updateWrapper);
        //删除该出入库信息
        return owinfoMapper.deleteById(id);
    }

}
